#ifndef REFEREE_H
#define REFEREE_H

#include "main.h"

typedef enum
{
    RED_HERO = 1,
    RED_ENGINEER = 2,
    RED_STANDARD_1 = 3,
    RED_STANDARD_2 = 4,
    RED_STANDARD_3 = 5,
    RED_AERIAL = 6,
    RED_SENTRY = 7,
    BLUE_HERO = 101,
    BLUE_ENGINEER = 102,
    BLUE_STANDARD_1 = 103,
    BLUE_STANDARD_2 = 104,
    BLUE_STANDARD_3 = 105,
    BLUE_AERIAL = 106,
    BLUE_SENTRY = 107,
} robot_id_t;

typedef enum
{
    PROGRESS_UNSTART = 0,
    PROGRESS_PREPARE = 1,
    PROGRESS_SELFCHECK = 2,
    PROGRESS_5sCOUNTDOWN = 3,
    PROGRESS_BATTLE = 4,
    PROGRESS_CALCULATING = 5,
} game_progress_t;

typedef __packed struct // 0x0001
{
    uint8_t game_type : 4;
    uint8_t game_progress : 4;
    uint16_t stage_remain_time;
    uint64_t SyncTimeStamp;
} ext_game_status_t;

typedef __packed struct // 0x0002
{
    uint8_t winner;
} ext_game_result_t;

typedef __packed struct // 0x0003
{
    uint16_t red_1_robot_HP;
    uint16_t red_2_robot_HP;
    uint16_t red_3_robot_HP;
    uint16_t red_4_robot_HP;
    uint16_t red_5_robot_HP;
    uint16_t red_7_robot_HP;
    uint16_t red_outpost_HP;
    uint16_t red_base_HP;
    uint16_t blue_1_robot_HP;
    uint16_t blue_2_robot_HP;
    uint16_t blue_3_robot_HP;
    uint16_t blue_4_robot_HP;
    uint16_t blue_5_robot_HP;
    uint16_t blue_7_robot_HP;
    uint16_t blue_outpost_HP;
    uint16_t blue_base_HP;
} ext_game_robot_HP_t;

typedef __packed struct // 0x0005
{
    uint8_t F1_zone_status : 1;
    uint8_t F1_zone_buff_debuff_status : 3;
    uint8_t F2_zone_status : 1;
    uint8_t F2_zone_buff_debuff_status : 3;
    uint8_t F3_zone_status : 1;
    uint8_t F3_zone_buff_debuff_status : 3;
    uint8_t F4_zone_status : 1;
    uint8_t F4_zone_buff_debuff_status : 3;
    uint8_t F5_zone_status : 1;
    uint8_t F5_zone_buff_debuff_status : 3;
    uint8_t F6_zone_status : 1;
    uint8_t F6_zone_buff_debuff_status : 3;
    uint16_t red1_bullet_left;
    uint16_t red2_bullet_left;
    uint16_t blue1_bullet_left;
    uint16_t blue2_bullet_left;
    uint8_t lurk_mode;
    uint8_t res;
} ext_ICRA_buff_debuff_zone_and_lurk_status_t;

typedef __packed struct // 0x0101
{
    uint32_t event_type;
} ext_event_data_t;

typedef __packed struct // 0x0102
{
    uint8_t supply_projectile_id;
    uint8_t supply_robot_id;
    uint8_t supply_projectile_step;
    uint8_t supply_projectile_num;
} ext_supply_projectile_action_t;

typedef __packed struct // 0x0103
{
    uint8_t supply_projectile_id;
    uint8_t supply_robot_id;
    uint8_t supply_num;
} ext_supply_projectile_booking_t;

typedef __packed struct // 0x0104
{
    uint8_t level;
    uint8_t offending_robot_id;
    uint8_t count;
} ext_referee_warning_t;

typedef __packed struct // 0x0105
{
    uint8_t dart_remaining_time;
    uint16_t dart_info;
} ext_dart_remaining_time_t;

typedef __packed struct // 0x0201
{
    uint8_t robot_id;
    uint8_t robot_level;
    uint16_t current_HP;
    uint16_t maximum_HP;
    uint16_t shooter_barrel_cooling_value;
    uint16_t shooter_barrel_heat_limit;
    uint16_t chassis_power_limit;
    uint8_t power_management_gimbal_output : 1;
    uint8_t power_management_chassis_output : 1;
    uint8_t power_management_shooter_output : 1;
} ext_game_robot_status_t;

typedef __packed struct // 0x202
{
    uint16_t chassis_volt;
    uint16_t chassis_current;
    fp32 chassis_power;
    uint16_t buffer_energy;
    uint16_t shooter_id1_17mm_cooling_heat;
    uint16_t shooter_id2_17mm_cooling_heat;
    uint16_t shooter_id1_42mm_cooling_heat;
} ext_power_heat_data_t;

typedef __packed struct // 0x0203
{
    fp32 x;
    fp32 y;
    fp32 angle;
} ext_game_robot_pos_t;

typedef __packed struct // 0x0204
{
    uint8_t recovery_buff;
    uint8_t cooling_buff;
    uint8_t defence_buff;
    uint8_t vulnerability_buff;
    uint16_t attack_buff;
    uint8_t remaining_energy;
} ext_buff_t;

typedef __packed struct // 0x0205
{
    uint8_t energy_point;
    uint8_t attack_time;
} aerial_robot_energy_t;

typedef __packed struct // 0x0206
{
    uint8_t armor_id : 4;
    uint8_t HP_deduction_reason : 4;
} ext_robot_hurt_t;

typedef __packed struct // 0x0207
{
    uint8_t bullet_type;
    uint8_t shooter_number;
    uint8_t launching_frequency;
    fp32 initial_speed;
} ext_shoot_data_t;

typedef __packed struct // 0x0208
{
    uint16_t projectile_allowance_17mm;
    uint16_t projectile_allowance_42mm;
    uint16_t remaining_gold_coin;
} ext_bullet_remaining_t;

typedef __packed struct // 0x0209
{
    uint32_t rfid_status;
} ext_rfid_status_t;

typedef __packed struct // 0x020A
{
    uint8_t dart_launch_opening_status;
    uint8_t reserved;
    uint16_t target_change_time;
    uint16_t latest_launch_cmd_time;
} ext_dart_client_t;

typedef  __packed struct // 0x020B
{
    fp32 hero_x;
    fp32 hero_y;
    fp32 engineer_x;
    fp32 engineer_y;
    fp32 standard_3_x;
    fp32 stantard_3_y;
    fp32 standard_4_x;
    fp32 standard_4_y;
    fp32 reserved_x;
    fp32 reserved_y;
} ext_ground_robot_position_t;

typedef __packed struct // 0x020C
{
    uint8_t mark_progress;
} ext_radar_mark_data_t;;

typedef __packed struct // 0x020D
{
    uint32_t sentry_info;
    uint16_t sentry_info_2;
} ext_sentry_info_t;

typedef __packed struct // 0x020E
{
    uint8_t radar_info;
} ext_radar_info_t;

typedef __packed struct // 0x0301
{
    uint16_t data_cmd_id;
    uint16_t sender_id;
    uint16_t receiver_id;
    uint8_t user_data[112];
} ext_student_interactive_header_data_t;

typedef __packed struct // 0x0303
{
    fp32 target_position_x;
    fp32 target_position_y;
    uint8_t cmd_keyboard;
    uint8_t target_robot_id;
    uint8_t cmd_source;
} ext_map_data_t;

typedef __packed struct
{
    fp32 data1;
    fp32 data2;
    fp32 data3;
    uint8_t data4;
} custom_data_t;

typedef __packed struct
{
    uint8_t data[64];
} ext_up_stream_data_t;

typedef __packed struct
{
    uint8_t data[32];
} ext_download_stream_data_t;

typedef struct
{
    // --- bit 0-2: 补给区占领状态 ---
    bool_t supply_zone_non_overlap;     // bit0: 已方与兑换区不重叠的补给区占领状态
    bool_t supply_zone_overlap;         // bit1: 已方与兑换区重叠的补给区占领状态
    bool_t supply_zone_occupied;        // bit2: 已方补给区占领状态（仅RMUL适用）

    // --- bit 3-5: 能量相关状态 ---
    bool_t small_energy_activated;      // bit3: 小能量激活状态
    bool_t large_energy_activated;      // bit4: 大能量激活状态
    uint8_t central_highland_status;    // bit5-6: 中央高地状态（0-3）

    // --- bit 7-8: 梯形高地状态 ---
    bool_t trapezoid_highland_occupied; // bit7-8: 梯形高地占领状态

    // --- bit 9-17: 飞镖击中时间 ---
    uint16_t last_hit_time;             // bit9-17: 对方飞镖最后一次击中时间（0-420）

    // --- bit 18-20: 飞镖击中目标类型 ---
    uint8_t last_hit_target;            // bit18-20: 击中目标类型（0-7）

    // --- bit 21-22: 中心增益点状态 ---
    uint8_t center_zone_status;         // bit21-22: 中心增益点状态（0-3）

    // --- bit 23-31: 保留位 ---
    uint16_t reserved;                  // bit23-31: 保留位
} battle_status_t;

extern void init_referee_struct_data(void);
extern void referee_data_solve(uint8_t *frame);

extern void get_chassis_power_and_buffer(fp32 *power, uint16_t *buffer);

extern void get_robot_id(uint16_t *robot_id, uint16_t *client_id);

extern void robot_id(uint16_t *robot_id);

extern void get_shoot_heat1_limit_and_heat1(uint16_t *heat1_limit, uint16_t *heat1);
extern void get_shoot_heat2_limit_and_heat2(uint16_t *heat2_limit, uint16_t *heat2);

extern void get_robot_hurt(uint8_t *armor_id, uint8_t *hurt_type);

extern void get_chassis_power_limit(uint16_t *power_limit);

extern void get_robot_level(uint8_t *level);

extern void get_ammo(uint16_t *bullet_remaining);

extern void get_sentry_HP(uint16_t *sentry_HP);

extern fp32 armor_direction(uint8_t armor_type);

extern void referee_battle_status_update(battle_status_t *referee_battle_status);

extern ext_game_status_t game_state_t;                                         // 0x0001
extern ext_game_result_t game_result_t;                                        // 0x0002
extern ext_game_robot_HP_t game_robot_HP_t;                                    // 0x0003

extern ext_event_data_t field_event_t;                                         // 0x0101
extern ext_supply_projectile_action_t supply_projectile_action_t;              // 0x0102
extern ext_supply_projectile_booking_t supply_projectile_booking_t;            // 0x0103
extern ext_referee_warning_t referee_warning_t;                                // 0x0104
extern ext_dart_remaining_time_t dart_remaining_time;                          // 0x0105

extern ext_game_robot_status_t robot_state_t;                                  // 0x0201
extern ext_power_heat_data_t power_heat_data_t;                                // 0x0202
extern ext_game_robot_pos_t game_robot_pos_t;                                  // 0x0203
extern ext_buff_t buff_musk_t;                                                 // 0x0204
extern aerial_robot_energy_t robot_energy_t;                                   // 0x0205
extern ext_robot_hurt_t robot_hurt_t;                                          // 0x0206
extern ext_shoot_data_t shoot_data_t;                                          // 0x0207
extern ext_bullet_remaining_t bullet_remaining_t;                              // 0x0208
extern ext_rfid_status_t rfid_status_t;                                        // 0x0209
extern ext_dart_client_t dart_client_t;                                        // 0x020A
extern ext_ground_robot_position_t ground_robot_position_t;                    // 0x020B
extern ext_radar_mark_data_t radar_mark_data_t;                                // 0x020C
extern ext_sentry_info_t sentry_info_t;                                        // 0x020D
extern ext_radar_info_t radar_info_t;                                          // 0x020E
extern ext_student_interactive_header_data_t student_interactive_data_t;
extern ext_map_data_t map_data_t;

extern battle_status_t battle_status;

extern const ext_game_robot_status_t *get_game_robot_status_point(void);
extern const ext_robot_hurt_t *get_robot_hurt_point(void);

#endif
